package com.appspot.moviesessions.infrastructure.persistence;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.jdo.annotations.Unique;

import com.appspot.moviesessions.model.Location;
import com.google.appengine.api.datastore.Key;

import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class LocationRecord implements Location {

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key key;

	@Persistent
	@Unique
	private String name;

	public LocationRecord() {

	}

	public LocationRecord(String locationName) {
		this.name = locationName;
	}

	public String getName() {
		return name;
	}

	@Override
	public Long getId() {
		return (key == null) ? null : key.getId();
	}

	@Override
	public boolean equals(final Object other) {
		if (!(other instanceof LocationRecord))
			return false;
		LocationRecord castOther = (LocationRecord) other;
		return new EqualsBuilder().append(name, castOther.name).isEquals();
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder().append(name).toHashCode();
	}

	public Key getKey() {
		return key;
	}

}
